<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on"
      label-position="left">
      <div class="title-container">
        <h3 class="title">BRF 管理后台</h3>
      </div>

      <el-form-item prop="mobile">
        <div class="pbox">
          <el-input v-model="loginForm.countryNo" class="areacode" placeholder="区号" />

          <el-input ref="mobile" v-model="loginForm.mobile" placeholder="手机号" />
        </div>

      </el-form-item>

      <el-form-item prop="code">

        <el-input v-model="loginForm.code" placeholder="验证码">
          <template slot="append">
            <p @click="getcode">{{ codeMessage }}</p>
          </template>
        </el-input>
      </el-form-item>

      <el-button :loading="loading" type="primary" style="width: 100%; margin-bottom: 30px"
        @click.native.prevent="handleLogin">登录</el-button>
    </el-form>
  </div>
</template>

<script>
import { smsCode } from '@/api/table'
import codeJson from '@/utils/code.js'
export default {
  name: 'Login',
  data() {
    return {
      activeName: '1',
      dialogVisible: false,
      loginForm: {
        mobile: '',
        countryNo: '',
        code: ''
      },
      loginRules: {
        mobile: [{ required: true, trigger: 'blur', message: '请输入手机号' }],
        countryNo: [{ required: true, trigger: 'blur', message: '请输入区号' }],
        code: [{ required: true, trigger: 'blur', message: '请输入验证码' }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      bok: true,
      codeMessage: '获取验证码',
      arr: []
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    select(i) {
      this.loginForm.countryNo = i.code
      this.dialogVisible = false
    },
    handleShow() {
      console.log(codeJson)
      this.arr = codeJson.list
      this.dialogVisible = true
    },
    async getcode() {
      if (!this.loginForm.mobile) {
        this.$confirm('请输入登录手机号?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        return false
      }
      if (!this.loginForm.countryNo) {
        this.$confirm('请输入区号?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        return false
      }

      if (!this.bok) return false
      this.bok = false
      const { code } = await smsCode({ mobile: this.loginForm.mobile, countryNo: this.loginForm.countryNo })
      if (code === 200) {
        let n = 60
        const timer = setInterval(() => {
          n--
          this.codeMessage = `${n}秒后重试`
          if (n <= 0) {
            n = 60
            this.bok = true
            clearInterval(timer)
            this.codeMessage = `获取验证码`
          }
        }, 1000)
      } else {
        this.bok = true
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        console.log(valid)
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || '/' })
            window.location.reload()
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

.item {
  display: flex;
  flex-wrap: wrap;

  span {
    padding: 10px 15px;
    cursor: pointer;

  }
}

.countryNo {
  padding: 0;
  margin: 0;
  color: #fff;
  display: inline;
  margin-left: 15px;
  cursor: pointer;
}

/* reset element-ui css */
.login-container {
  .el-input-group__append {
    background: transparent;
    color: #409EFF;
    border: 0px;
    cursor: pointer;
  }

  .pbox {
    display: flex;

    .areacode {
      width: 80px;
      display: flex;
      align-items: center;

      &::after {
        width: 1px;
        height: 20px;
        background: #8e8686;
        display: block;
        content: "";
      }
    }
  }

  .el-input {
    >.el-input-group__prepend {
      background: transparent;
      border: 0px;
      color: #fff;
    }

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
